package com.xsp.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xsp
 * @date 2024/2/25
 * @desc 登录验证拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 查看是都有登录凭证
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            // 没有登录凭证
            response.sendRedirect(request.getContextPath() + "/login");
            // 不能放行
            return false;
        }
        // 有，放行
        return true;
    }
}
